﻿package tbo.game 
{
	import flash.events.EventDispatcher;
	import tbo.actors.Actor;
	import tbo.events.TBOActorEvent;
	import tbo.game.attacks.AttackArray;
	import tbo.game.attacks.AttackLink;
	import tbo.world.TacticalStage;
	
	/**
	 * ...
	 * @author ...
	 */
	public class BattleManager extends EventDispatcher
	{

		public var state:String;
		public var _tacticalStage:TacticalStage;
		public var attackArray:AttackArray;
		public var playerTurn:Number;
		public var selectedUnit:Actor;
		
		public function BattleManager() 
		{
			playerTurn = 0;
			state = "wait";
			_tacticalStage = new TacticalStage(this);
			attackArray = new AttackArray();
		}
		
		public function beginTurn():void {
			attackArray = new AttackArray();
		}
		
		public function executeTurn():void {
			trace('executing');
			attackArray.execute();
			attackArray = null;
			attackArray = new AttackArray();
		}
		
		public function handleActorClick(a:Actor):void {
			switch(this.state) {
				case "wait":
					selectedUnit = a;
					dispatchEvent(new TBOActorEvent(TBOActorEvent.ACTOR_CLICKED, a));
					break;
				case "target":
					var link:AttackLink = new AttackLink(selectedUnit._attacks[0], a, selectedUnit);
					attackArray.addAttack(link);
					trace("Trace link");
					setState("wait");
					break;
				case "move":
					break;
			}
		}
		
		public function setState(stat:String):void {
			trace("new state:" + stat);
			state = stat;
		}
		
	}
	
}